﻿using ProjectFootball.MVC.Areas.TeamArea.Models;
using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public static class _TeamVersionExtensions
    {
        public static TeamDataModel GetDataModel(this TeamVersion teamVersion)
        {
            return new TeamDataModel()
            {
                TeamVersionGuid = teamVersion.Pk,
                TeamGuid = teamVersion.TeamGuid,
                ApprovalGuid = teamVersion.ApprovalGuid,
                TeamName = teamVersion.TeamName,
                CountryGuid = teamVersion.CountryGuid,
                VenueGuid = teamVersion.HomeVenueGuid,
                ActiveFrom = teamVersion.ActiveFrom,
                ActiveTo = teamVersion.ActiveTo
            };
        }

        public static IEnumerable<TeamSearchResultViewModel> Search(this IQueryable<TeamVersion> teamVersions, IPrincipal user, SearchType searchType)
        {
            var query = teamVersions.Where(t => !t.IsMarkedForDeletion).AsEnumerable().Where(c => c.CanView(user)).OrderBy(c => c.TeamName);

            foreach (TeamVersion teamVersion in query)
                yield return teamVersion.GetSearchResultViewModel(user, searchType);
        }

        public static TeamSearchResultViewModel GetSearchResultViewModel(this TeamVersion teamVersion, IPrincipal user, SearchType searchType)
        {
            return new TeamSearchResultViewModel()
            {
                TeamVersion = teamVersion,
                CanCreateVersion = teamVersion.CanView(user),
                IsVisible = teamVersion.CanView(user),
                IsEditVisible = searchType == SearchType.Details ? teamVersion.CanView(user) : false,
                IsDeleteVisible = (searchType == SearchType.Details || (searchType == SearchType.RecordForApproval && !teamVersion.IsApproved)) ? teamVersion.CanDelete(user) : false,
                IsViewApprovalsVisible = (searchType == SearchType.Details || searchType == SearchType.PendingApprovals) ? teamVersion.CanApprove(user) : false,
                IsConfirmApprovalVisible = searchType == SearchType.RecordForApproval ? teamVersion.CanApprove(user) : false
            };
        }

        public static TeamViewModel GetTeamViewModel(this TeamVersion teamVersion, IPrincipal user, SearchType searchType)
        {
            return new TeamViewModel()
            {
                TeamSearchResultViewModel = GetSearchResultViewModel(teamVersion, user, searchType)
            };
        }

        public static void Edit(this TeamVersion teamVersion, TeamDataModel teamDataModel, IPrincipal user)
        {
            teamVersion.TeamGuid = teamDataModel.TeamGuid;
            teamVersion.ApprovalGuid = teamDataModel.ApprovalGuid;
            teamVersion.TeamName = teamDataModel.TeamName;
            teamVersion.CountryGuid = teamDataModel.CountryGuid;
            teamVersion.HomeVenueGuid = teamDataModel.VenueGuid;
            teamVersion.ActiveFrom = teamDataModel.ActiveFrom;
            teamVersion.ActiveTo = teamDataModel.ActiveTo;
            teamVersion.IsApproved = teamVersion.IsApproved ? user.IsApprover() : false;
            teamVersion.AuditUserId = user.GetUserId();
            teamVersion.AuditTimestamp = DateTime.Now;
        }

        public static void Approve(this TeamVersion teamVersion, TeamVersion previousApproved)
        {
            previousApproved.TeamName = teamVersion.TeamName;
            previousApproved.CountryGuid = teamVersion.CountryGuid;
            previousApproved.HomeVenueGuid = teamVersion.HomeVenueGuid;
            previousApproved.ActiveFrom = teamVersion.ActiveFrom;
            previousApproved.ActiveTo = teamVersion.ActiveTo;
            previousApproved.AuditUserId = teamVersion.AuditUserId;
            previousApproved.AuditTimestamp = teamVersion.AuditTimestamp;
        }
    }
}